package manage.modules.dept.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import manage.commons.utils.Pager;
import manage.commons.utils.TableNameUtil;
import manage.modules.BaseService;
import manage.modules.dept.model.Dept;
import manage.modules.dept.model.GroupOfDept;
import manage.modules.dept.service.DeptService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

public class DeptServiceImpl implements DeptService {
	private static final String DEPT_TABLE_PRIMARY = "deptId";
	private static final String DEPT_CACHE_KEY = "allDept";
	private BaseService baseService;

	@Override
	public Dept select(Long deptId) {
		return baseService.select(DEPT_TABLE_PRIMARY, deptId, getClazz());
	}

	@Override
	public int insert(Dept dept) {
		int count = baseService.insert(dept);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int update(Dept dept) {
		int count = baseService.updateFields(DEPT_TABLE_PRIMARY,
				dept.getDeptId(), dept);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int delete(Long deptId) {
		int count = baseService.delete(DEPT_TABLE_PRIMARY, deptId, getClazz());
		GroupOfDept groupOfDept = new GroupOfDept();
		groupOfDept.setDeptId(deptId);
		delete(groupOfDept);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public List<Dept> listDeptByGroupId(Long groupId) {
		List<GroupOfDept> gOfDepts = baseService.listByField("groupId",
				groupId, GroupOfDept.class);
		List<Dept> depts = new ArrayList<Dept>();
		for (GroupOfDept groupOfDept : gOfDepts) {
			Long deptId = groupOfDept.getDeptId();
			depts.add(select(deptId));
		}
		return depts;
	}

	@Override
	public List<Dept> listAll() {
		List<Dept> allDept = (List<Dept>) baseService.getListByCache(
				DEPT_CACHE_KEY, getClazz());
		if (CollectionUtils.isEmpty(allDept)) {
			allDept = baseService.listAll(getClazz());
			if (CollectionUtils.isNotEmpty(allDept)) {
				baseService.setCache(DEPT_CACHE_KEY, allDept);
			}
		}
		return allDept;
	}

	@Override
	public boolean exists(Long deptId) {
		return baseService.exists(DEPT_TABLE_PRIMARY, deptId, getClazz());
	}

	@Override
	public boolean groupVested(Long deptId) {
		return baseService
				.exists(DEPT_TABLE_PRIMARY, deptId, GroupOfDept.class);
	}

	@Override
	public List<Dept> listByAbility(int ability) {
		return baseService.listByField("ability", ability, getClazz());
	}

	@Override
	public void query(Dept dept, Pager<Dept> pager) {
		StringBuilder querySql = new StringBuilder(1024);
		querySql.append("select * from "
				+ TableNameUtil.getTableName(getClazz()) + " where 1=1");
		if (dept != null) {
			if (null != dept.getDeptId()) {
				querySql.append(" and deptId =" + dept.getDeptId());
			}
			if (StringUtils.isNotBlank(dept.getDeptName())) {
				querySql.append(" and deptName like '%" + dept.getDeptName()
						+ "%'");
			}
			if (null != dept.getAbility()) {
				querySql.append(" and ability =" + dept.getAbility());
			}
		}
		pager.setRowCount(baseService.getJdbcOperations().queryForLong(
				baseService.getCountSql(querySql.toString())));
		querySql.append(" limit ?,?");
		ParameterizedRowMapper<Dept> rowMapper = ParameterizedBeanPropertyRowMapper
				.newInstance(getClazz());
		pager.setList(baseService.getJdbcOperations().query(
				querySql.toString(), rowMapper, pager.getRowOffset(),
				pager.getPageSize()));
	}

	@Override
	public int insert(GroupOfDept groupOfDept) {
		return baseService.insert(groupOfDept);
	}

	@Override
	public void insertGroupOfDepts(Long groupId, List<Long> deptIds) {
		if (!CollectionUtils.isEmpty(deptIds)) {
			for (Long deptId : deptIds) {
				GroupOfDept groupOfDept = new GroupOfDept();
				groupOfDept.setAutoId(0);
				groupOfDept.setGroupId(groupId);
				groupOfDept.setDeptId(Long.valueOf(deptId));
				insert(groupOfDept);
			}
		}
	}

	@Override
	public int delete(GroupOfDept groupOfDept) {
		Map<String, Object> gOfDept = baseService.comparePojo(null, groupOfDept);
		return baseService.deleteByFields(gOfDept, GroupOfDept.class);
	}

	@Override
	public void deleteGroupOfDepts(Long groupId, List<Long> deptIds) {
		if (CollectionUtils.isNotEmpty(deptIds)) {
			for (Long deptId : deptIds) {
				GroupOfDept groupOfDept = new GroupOfDept();
				groupOfDept.setGroupId(groupId);
				groupOfDept.setDeptId(Long.valueOf(deptId));
				delete(groupOfDept);
			}
		}
	}

	@Override
	public Long getCountOfAll() {
		return baseService.countOfAll(getClazz());
	}

	private void clearCacheByCount(int count) {
		if (count > 0) {
			baseService.clearCache(DEPT_CACHE_KEY);
		}
	}

	public Class<Dept> getClazz() {
		return Dept.class;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
